<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_admin extends MY_Admin {
    function __construct()
    {
        parent :: __construct();
        $this->lang->load('user', 'chinese');
        $this->load->model('User_model');
    }

    public function index()
    {
        $config['base_url']         = site_url('admins/user_admin');
        $config['total_rows']       = 200;
        $config['per_page']         = $this->pageLimit;
        $config['uri_segment']      = 3;
        //$config['use_page_numbers'] = TRUE;
        //echo $this->uri->segment(3);
        $this->pagination->initialize($config);

        $datas['users'] = $this->User_model->get_users();
        $this->load->view('admins/user/base', $datas);
    }

    public function delete()
    {
        if($this->User_model->get_one($this->uri->segment(4)))
            if($this->User_model->delete_user($this->uri->segment(4)))
                $this->goback(1);
            else
                $this->goback(0);
        else
            $this->goback(0);
    }
    
    public function edit()
    {
        $datas['user'] = $this->User_model->get_one($this->uri->segment(4));
        $this->load->view('admins/user/edit',$datas);
    }
    
    public function doEdit()
    {
        // Validation rules
        $validation = array(
            array(
                'field' => 'mail',
                'label' => lang('email'),
                'rules' => 'required|trim|valid_email'
            ),
            array(
                'field' => 'password',
                'label' => lang('password'),
                'rules' => 'trim|min_length[5]|max_length[20]'
            ),
            array(
                'field' => 'screenName',
                'label' => lang('screenName'),
                'rules' => 'required|trim'
            )
        );

        // Set the validation rules
        $this->form_validation->set_rules($validation);
        $user_data = array();
        // Convert the array to an object
        $user_data['mail']			    = $this->security->xss_clean($this->input->post('mail'));
        $user_data['screenName'] 		= $this->security->xss_clean($this->input->post('screenName'));
        $user_data['password']          = $this->security->xss_clean($this->input->post('password'));
        $user_data['group']             = $this->security->xss_clean($this->input->post('group'));
        $user_data['activated']   	    = $this->input->post('activated');
        $user_data['uid']   	        = $this->input->post('uid');
        if ($this->form_validation->run())
        {
            //$this->Entries->add($this->input->post());
            if($this->User_model->add($user_data))
                $this->goback(1);
            else
                $this->goback(0);
        }
        else
        {
            echo validation_errors(); 
        }
    }
    
}